package com.ants.boot.utils;

import java.util.Random;

/**
 * @author 蚂蚁会花呗
 * @date 2022/9/1 10:32
 */
public class PhoneCodeUtils {


    /**
     * 保存到redis中的验证码缓存的前缀
     */
    public static final String LOGIN_START_WITH = "username_phone:";

    /**
     * 注册时redis中的验证码缓存的前缀
     */
    public static final String REG_START_WITH = "username_reg:";


    private static final String sources = "0123456789"; // 加上一些字母，就可以生成pc站的验证码了

    /**
     * 获取六位随机数字
     * @return
     */
    public static String getRandomCode(){
        Random rand = new Random();
        StringBuffer flag = new StringBuffer();
        for (int j = 0; j < 6; j++) {
            flag.append(sources.charAt(rand.nextInt(9)) + "");
        }
        return flag.toString();
    }

    /**
     * 保存到缓存中的验证码前缀
     * @param username
     * @return
     */
    public static String codeStartWith(String username){
        return LOGIN_START_WITH + username;
    }

    /**
     * 注册时短信验证码
     * @param username
     * @return
     */
    public static String regStartWith(String username){
        return REG_START_WITH + username;
    }

}
